package com.bckj.fastboot.upms.biz.core.user.event;

import com.bckj.fastboot.core.redis.RedisOps;
import com.bckj.fastboot.upms.api.constant.UpmsCacheConstant;
import com.bckj.fastboot.upms.api.entity.SysUser;
import com.bckj.fastboot.upms.biz.repository.SysUserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 用户信息缓存监听器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserCacheEventListener implements ApplicationListener<UserCacheEvent> {
    private final RedisOps redisOps;
    private final SysUserRepository sysUserRepository;

    @Override
    public void onApplicationEvent(UserCacheEvent event) {
        switch (event.getType()) {
            case 1:
                //sys user
                SysUser sysUser = sysUserRepository.getById((Long) event.getValue());
                sysUser.setPassword(null);
                redisOps.hset(UpmsCacheConstant.SYS_USER_KEY, sysUser.getId().toString(), sysUser);
                break;
        }
    }
}
